package edu.gyu.myshop.controller;

import edu.gyu.myshop.model.*;
import edu.gyu.myshop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MyorderController {
    @Autowired
    MyorderService orderService;

    @RequestMapping("/toOrder")
    public String dealOrder(HttpSession session, Model model) {
        Myuser user = (Myuser) session.getAttribute("user");
        if (user != null) {
            orderService.putOrder(session);
            List<Myorder> orders = orderService.lambdaQuery()
                    .eq(Myorder::getUserid, user.getId())
                    .orderByDesc(Myorder::getId)
                    .list();
            model.addAttribute("orders", orders);
            return "/order";
        }
        return "/";
    }

    @RequestMapping("/myOrder")
    public String myOrder(HttpSession session, Model model) {
        List<Myorder> orders = orderService.listOrder(session);
        String msg = "尚未提交订单！";
        if (orders != null && orders.size() > 0) {
            model.addAttribute("orders", orders);
            return "/order";
        } else {
            model.addAttribute("msg", msg);
            return "/";
        }
    }

    @Autowired
    UserAddressService userAddressService;

    @RequestMapping("/toPay")
    public String toPay(Integer orderId, Model model) {

        Myorder order = orderService.lambdaQuery()
                .eq(Myorder::getId, orderId)
                .one();

        int userId = order.getUserid();
        UserAddress address = userAddressService.lambdaQuery()
                .eq(UserAddress::getUserId, userId)
                .one();
        //freeamark取值，至少得有对象，对象的属性值可为空
        if (address == null) {
            address = new UserAddress();
        }
        model.addAttribute("order", order);
        model.addAttribute("address", address);
        return "/pay";


    }


    @RequestMapping("/pay")
    @Transactional
    public String pay(UserAddress userAddress, Integer orderId, Model model) {

        userAddressService.saveOrUpdate(userAddress);
        orderService.lambdaUpdate()
                .eq(Myorder::getId, orderId)
                .set(Myorder::getStatus, 1)
                .update();
        model.addAttribute("orderId", orderId);

        return "/ok";

    }

    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    TrackingNumbersService trackingNumbersService;


    @RequestMapping("/orderDetail/{id}")
    public String orderDetail(@PathVariable("id") int id, Model model) {
        OrderDetail orderDetail = orderDetailService.lambdaQuery()
                .eq(OrderDetail::getOrderid, id)
                .one();
        Myorder one = orderService.lambdaQuery().eq(Myorder::getId, id).one();
        TrackingNumbers trackingNumbers = trackingNumbersService.lambdaQuery().eq(TrackingNumbers::getOrderid, orderDetail.getId()).one();

        orderDetail.setProductName(goodsService.getById(orderDetail.getGoodsid()).getName());
        orderDetail.setStatus(String.valueOf(one.getStatus()));
        orderDetail.setTrackingNumber(trackingNumbers.getTrackingNumber());
        orderDetail.setShippingCompany(trackingNumbers.getShippingCompany());
        orderDetail.setAddress(userAddressService.lambdaQuery().eq(UserAddress::getUserId, orderDetail.getUserid()).one().getAddress());
        orderDetail.setPhone(userAddressService.lambdaQuery().eq(UserAddress::getUserId, orderDetail.getUserid()).one().getPhone());
        orderDetail.setPicture(goodsService.getById(orderDetail.getGoodsid()).getPicture());
        model.addAttribute("order", orderDetail);
        System.out.println(orderDetail);
        return "/orderDetail";
    }

    @RequestMapping("/confirmReceipt/{id}")
    public String confirmReceipt(@PathVariable("id") int id, Model model) {
        OrderDetail orderDetail = orderDetailService.getById(id);
        Integer orderid = orderDetail.getOrderid();
        Myorder myorder = orderService.lambdaQuery().eq(Myorder::getId, orderid).one();
        myorder.setStatus(3);
        orderService.updateById(myorder);
        model.addAttribute("msg", "收货成功");
        return "redirect:/myOrder";
    }
}